package com.lipaluma.xml.merge;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

import com.lipaluma.exceptions.CsvParserException;
import com.lipaluma.field.merge.TargetMergedAlgorithm;

@XmlType(name=XmlTargetMerged.TARGET_MERGED)
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlTargetMerged {
	public static final String TARGET_MERGED = "target-merged";
	
	@XmlAttribute(name="with")
	private Class<? extends TargetMergedAlgorithm> targetMergedAlgorithm;

	@XmlElement(name="param")
	private String[] params;

	public TargetMergedAlgorithm getAlgorithm() {
		if(targetMergedAlgorithm == null)
			return null;
		try {
			return targetMergedAlgorithm.newInstance();
		} catch (InstantiationException e) {
			throw new CsvParserException("The algorithm merge "+targetMergedAlgorithm+" can not be instanciated", e);
		} catch (IllegalAccessException e) {
			throw new CsvParserException("The algorithm merge "+targetMergedAlgorithm+" can not be accessed", e);
		}
	}

	public String[] getParams() {
		if(this.params == null)
			return new String[0];
		return this.params;
	}
}
